#!/bin/bash

# use: question <text> <varname_result>
function question() {
[ "$#" -lt 2 ] && return 1
text="$1"
flag="unknown"
while [ "$flag" != "Y" -a "$flag" != "N" ]
do
    echo -ne "$text [Y/N] "
    read flag
    case $flag in
    "Y"|"y")    flag="Y"
                ;;
    "N"|"n")    flag="N"
                ;;
    esac
done
[ ! -z "$2" ] && eval $2="\"$flag\""
return 0
}

cd `dirname $0`
echo "check directory ..."
[ ! -d "$HOME" ] && { echo "HOME dir \"$HOME\" not exist. exit."; exit 1; }
dir_list=( "common" "conf" "logs" "misc" "data" "notification" "scripts" "tools" "tools/logs")
for dir in ${dir_list[@]}
do
[ ! -d "$dir" ] && { echo "not find directory \"$dir\". exit."; exit 1; }
done

question "generate ssh-keys for host authentification?" qres
if [ "$qres" = "Y" ]; then
    ./tools/init_ssh.sh
fi

export OPENARS_HOME=$PWD
echo "export OPENARS_HOME=$OPENARS_HOME" | tee -a $HOME/.profile
./tools/svn2work.sh

echo "finish!"
echo "to begin run \"source \$HOME/.profile\" and modify xml-files in directory \"conf\""

exit 0
